package com.hxkj.controller.taglib;

import com.hxkj.client.service.taglib.AnalyzeChannelService;
import com.hxkj.domain.po.FreeMarker;
import com.integral.mall.file.po.SysFile;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * @ClassName AnalyzeColumnController
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/7/18 17:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/analyze")
public class AnalyzeChannelController {

    @Autowired
    private AnalyzeChannelService analyzeChannelService;


    /**
     * @author Mr.zheng
     * @Description analyzeColumn 解析文章分类自定义标签
     * @Date 17:06 2018/7/18
     **/
    @RequestMapping(value = "/analyzeChannel")
    public Callable<ResultDataStruct<SysFile>> analyzeChannel(@RequestBody FreeMarker freeMarker){
        return () -> {
            ResultDataStruct<SysFile> resultDataStruct = new ResultDataStruct<>();
            try {
                SysFile sysFile = analyzeChannelService.analyzeChannel(freeMarker);
                resultDataStruct.setData(sysFile);
                resultDataStruct.setCode(200);
                resultDataStruct.setMessage("成功");
            }catch (CustomException e){
                resultDataStruct.setCode(e.getCode().getCode());
                resultDataStruct.setMessage(e.getMessage());
            }catch (CustomMessageException e){
                resultDataStruct.setCode(e.getCode());
                resultDataStruct.setMessage(e.getMessage());
            }
            return resultDataStruct;
        };
    }

    /**
     * @author Mr.zheng
     * @Description analyzeColumn 解析热门文章及其推荐文章列表自定义标签
     * @Date 17:06 2018/7/18
     * @Param [freeMarker]
     * @return com.platform.common.domain.result.ResultDataStruct
     **/
    @RequestMapping(value = "/analyzeHotArticle")
    public Callable<ResultDataStruct<SysFile>> analyzeHotArticle(@RequestBody FreeMarker freeMarker) {
        return () -> {
            ResultDataStruct<SysFile> resultDataStruct = new ResultDataStruct<>();
            try {
                SysFile sysFile = analyzeChannelService.analyzeHotArticle(freeMarker);
                resultDataStruct.setData(sysFile);
                resultDataStruct.setCode(200);
                resultDataStruct.setMessage("成功");
            }catch (CustomException e){
                resultDataStruct.setCode(e.getCode().getCode());
                resultDataStruct.setMessage(e.getMessage());
            }catch (CustomMessageException e){
                resultDataStruct.setCode(e.getCode());
                resultDataStruct.setMessage(e.getMessage());
            }
            return resultDataStruct;
        };
    }
}
